<template>
  <div class="login-container">
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="user.mobile" name="手机号" placeholder="请输入手机号"
        :rules="userFormRules.mobile" type='number' maxlength='11'>
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field v-model="user.code" left-icon="smile-o" name="密码"
        placeholder="请输入验证码" :rules="userFormRules.code" type='number'
        maxlength='6'>
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down :time="1000 * 60" format='sss后发送' v-if="isCountShow"
            @finish='isCountShow = false' />
          <van-button class="send-sms-btn" round size="small" type="default"
            native-type="button" @click="onSendSms" v-else>
            发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'

export default {
  name: 'LoginIndex',
  data () {
    return {
      user: { // 用户登录的手机号 验证码
        mobile: '',
        code: ''
      },
      userFormRules: { // 表单验证的数据结构
        mobile: [{
          required: true,
          message: '手机号不能为空'
        },
        {
          pattern: /^1[3|5|7|8|9]\d{9}$/,
          message: '手机号错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        },
        {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isCountShow: false // 是否展示倒计时
    }
  },
  methods: {
    async onSubmit () {
      // 获取数据
      const user = this.user
      // 验证
      // 登录的提示框
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true,
        duration: 1000 * 30,
        closeOnClick: true
      })
      // 提交
      try {
        const { data } = await login(user)
        // console.log('登录成功', res)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登陆成功')
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) {
          // console.log('手机或者验证码错误')
          this.$toast.fail('手机或者验证码错误')
        } else {
          console.log('登录失败,别登了', err)
          this.$toast.fail('登录失败,别登了', err)
        }
      }
      // 后续操作
    },
    // 获取验证码
    async onSendSms () {
      // 校验手机号码
      try {
        await this.$refs.loginForm.validate('手机号')
        this.$toast('发送成功')
        // console.log('验证通过')
      } catch (err) {
        // return console.log('验证失败', err)
        return this.$toast('验证失败', err)
      }
      // 通过了显示倒计时
      this.isCountShow = true
      // 请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
        // console.log('发送成功')
      } catch (err) {
        this.isCountShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了')
        } else {
          return this.$toast('发送失败')
          // console.log('发送失败')
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login-container {
  .iconfont {
    font-size: 37px;
  }
  //按钮背景色
  .send-sms-btn {
    background-color: #ededed;
    width: 153px;
    height: 46px;
    line-height: 46px;
    color: #666;
    padding: 0;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background: #6db4fb;
      border: none;
    }
  }
}
</style>
